package com.sjf.day02.Demo;

/**
 * builder模式实现，链式调用
 * @author <huangtang>
 * @since 2021/12/22 14:19
 **/
public class Person {

    public static void main(String[] args) {
        Builder builder = new Builder(1);
        Person person = builder.setSex(23)
                .setName(1)
                .setTel(178).build();
        System.out.println(person.toString());
    }


    private final int id; //必须
    private final int age;
    private final int sex;
    private final int name;
    private final int tel;

    private Person(Builder builder){
        this.id = builder.id;
        this.age = builder.age;
        this.sex = builder.sex;
        this.name = builder.name;
        this.tel = builder.tel;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", age=" + age +
                ", sex=" + sex +
                ", name=" + name +
                ", tel=" + tel +
                '}';
    }

    public static class Builder{
        private int id;
        private int age;
        private int sex;
        private int name;
        private int tel;

        public Builder(int id){
            this.id = id;
        }
        public Builder setAge(int age){
            this.age = age;
            return this;
        }
        public Builder setSex(int sex){
            this.sex = sex;
            return this;
        }
        public Builder setName(int name){
            this.name = name;
            return this;
        }
        public Builder setTel(int tel){
            this.tel = tel;
            return this;
        }
        public Person build(){
            return new Person(this);
        }
    }


}
